<?php
class Default_UserController extends ZF_LoginController{
	
        public function loginAction() {
            if ($this->_request->isPost()) {

                $db = Zend_Registry::get("db");

                $auth = Zend_Auth::getInstance();

                $authAdapter = new Zend_Auth_Adapter_DbTable($db, "user", "username", "pw");

                $username = $this->_request->getParam('username');
                $password = $this->_request->getParam('pw');
            
                
                $authAdapter->setIdentity($username);
                $authAdapter->setCredential(md5($password));
                 
                $result = $auth->authenticate($authAdapter);

                if ($result->isValid()){
                    
                    $data = $authAdapter->getResultRowObject(null, array('password'));
                    
                    
                    $auth->getStorage()->write($data);
                    $this->_redirect('/default/user/profile');

                }else{
                   // $p = sha1(md5($password.'system'));
                    $this->view->error = "Your account is invalid";
                    echo "error";
                }
            
            }
            
        }
    
        public function logoutAction(){
            Zend_Auth::getInstance()->clearIdentity();
            $this->_redirect("/default/user/login");       
        }
        
        public function profileAction() {
            
            $auth = Zend_Auth::getInstance();
            
            $user = (array)$auth->getStorage()->read();
            $this->view->header = "Welcome back ".$user['fullname'];
            
            $Common = new Default_Model_Common();
            $this->view->city = $Common->CITY;
            if($this->getRequest()->isPost())
                {
                    
                    $_pw = $this->getRequest()->getParam('password');
                    $_re_pw = $this->getRequest()->getParam('re-password');
                    $_fullname = $this->getRequest()->getParam('fullname');
                    $_displayname = $this->getRequest()->getParam('displayName');
                    $_telephone = $this->getRequest()->getParam('phone');
                    $_email = $this->getRequest()->getParam('email');
                    $_address = $this->getRequest()->getParam('address');
                    $_city = $this->getRequest()->getParam('city');
                    if(isset($_pw) && $_pw == $_re_pw){
                        if ($_address && $_displayname && $_email && $_fullname 
                                && $_pw && $_telephone ) {
                            $model_user = new Default_Model_User();
                            $model_user->getByUsername($user['username']);
                            $model_user->setAddress($_address);
                            $model_user->setPw($_pw);
                            $model_user->setFullname($_fullname);
                            $model_user->setEmail($_email);
                            $model_user->setAddress($_address);
                            $model_user->setCity($_city);
                            $model_user->setFullname($_fullname);
                            
                            $result = $model_user->update();
                            if($result >0){
                                echo "Cập nhật thành công";
                            }else{
                                 $this->redirect("/index/error");
                            }
                            
                        } else {
                            $this->redirect("/index/error");
                        }
                    }
                }
            
        }
        
}
